﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient; // Connect to Database
using System.Net.Mail; // Send feedback form to email
using System.Net;
using System.Web.Security;
using System.Xml.Linq;

public partial class Contact_Us : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
  
    protected void ButtonSubmit_click(object sender, EventArgs e)
    {
        storeDatabase();
        sendmail();
    }

    protected void storeDatabase()
    {

        string connectionString = ConfigurationManager.ConnectionStrings["stockmarketConnectionString"].ConnectionString;
        string insertSql = "INSERT INTO [Feedback] (Username,Type,Description,Email,DateSubmitted ) values (@UserName,@Type,@Description,@Email,@DateSubmitted)";

        //Create SQL Connection 
        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = insertSql;


        SqlParameter UserName = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
        UserName.Value = User.Identity.Name;
        cmd.Parameters.Add(UserName);

        SqlParameter Type = new SqlParameter("@Type", SqlDbType.VarChar, 30);
        Type.Value = DropDownListCategory.Text.ToString();
        cmd.Parameters.Add(Type);

        SqlParameter Description = new SqlParameter("@Description", SqlDbType.Text);
        Description.Value = txtDescription.Text.ToString();
        cmd.Parameters.Add(Description);

        SqlParameter Email = new SqlParameter("@Email", SqlDbType.VarChar, 30);
        Email.Value = txtEmail.Text.ToString();
        cmd.Parameters.Add(Email);

        SqlParameter DateSubmitted = new SqlParameter("@DateSubmitted", SqlDbType.DateTime);
        DateSubmitted.Value = DateTime.Now;
        cmd.Parameters.Add(DateSubmitted);


        //SqlParameter Filename = new SqlParameter("@Filename", SqlDbType.VarChar, 250);
        //Filename.Value = FileUpload.Text.ToString();
        //cmd.Parameters.Add(Filename);

        try
        {
            con.Open();
            SqlCommand cmd3 = new SqlCommand("select * from [Feedback] where Type='" + User.Identity.Name + "'", con);
            SqlDataReader dr = cmd3.ExecuteReader();

            if (dr.Read())
            {
                dr.Close();
            }
            else
            {
                dr.Close();
                cmd.ExecuteNonQuery();

            }
        }

        catch (SqlException ex)
        {
            string errorMessage = "Error in Feedback Form.";
            errorMessage += ex.Message;
            throw new Exception(errorMessage);

        }

        finally
        {
            con.Close();
        }

        // MessageBox alert user the feedback have send sucessfully           
        Response.Write("<script language='javascript'>alert('Thank you for your feedback');window.location.href='Contact Us.aspx'</script>");

    }

    protected void sendmail()
    {
        SmtpClient smtpServer = new SmtpClient();
        MailMessage message = new MailMessage();

        MailAddress fromAddress = new MailAddress("stockmarketsimulation@hotmail.com");

        //Specify the host name or ipaddress of the server            
        smtpServer.Host = "smtp.live.com";
        smtpServer.EnableSsl = true;

        //Specify smtp port 
        smtpServer.Port = 587;

        //From address will be given as a MailAddress Object
        message.From = fromAddress;

        // To address collection of MailAddress
        message.To.Add("stockmarketsimulation@hotmail.com");
        message.Subject = DropDownListCategory.Text;

        //Body can be Html or text format
        //Specify true if it  is html message
        message.IsBodyHtml = false;

        // Message body content
        message.Body += "Catergory: " + DropDownListCategory.Text + "\n";
        message.Body += "Description: " + txtDescription.Text + "\n";
        message.Body += "Email: " + txtEmail.Text + "\n";

        // Send SMTP mail
        smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpServer.Credentials = new NetworkCredential("stockmarketsimulation@hotmail.com", "poiu1234");
        smtpServer.Send(message);

        // MessageBox alert user the feedback have send sucessfully           
        Response.Write("<script language='javascript'>alert('Thank you for your feedback');window.location.href='Contact Us.aspx'</script>");             
                        
    }

    public SqlParameter UserName { get; set; }
}